<?php
namespace app\controller\back;

use app\common\controller\_AdminController;
use app\model\Log;
use think\Validate;

class LogController extends _AdminController
{	
	protected $rule = [
		'user_name|账号'		=> 'alphaDash|length:1,50',
		'user_type|类型'     => 'in:1,2',
		'path|路径'          => 'length:1,100',
        'start|起始时间'		=> 'dateFormat:Y-m-d',
        'end|结束时间'		=> 'dateFormat:Y-m-d',
        'page|页码'         	=> 'integer',
        'size|条数'        	=> 'integer',
	];
	
	protected $scene = [
        'list'              => ['user_name','user_type','path','start','end','page','size'],
	];

	protected function initialize()
	{	
		parent::initialize();
		switch ($this->action) {
			default:
				# code...
				break;
		}
		$this->check();
	}
	
	/**
	 * @api {POST} /back/log/list 配置列表
	 * @apiVersion 0.0.1
	 * @apiGroup Back-log
	 * 
	 * @apiParam {String} [alias] 别名
	 * @apiParam {String} [user_name] 账号
	 * @apiParam {Number} [user_type] 账号
	 * @apiParam {String} [path] 路径
	 * @apiParam {String} [start] 开始时间
     * @apiParam {String} [end] 结束时间
	 * @apiParam {Number} [page=1] 页码
	 * @apiParam {Number} [size=10] 每页条数
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": [{
     *         "id": 39,
	 *         "alias": "zcx",
	 *         "name": "ad222",
	 *         "sort": "1",
	 *         "desc": "asds",
	 *         "value": "1",
     *         "type": "number",  
     *         "create_time": "2019-04-09 17:24:19",
     *         "update_time": "2019-04-09 17:24:19",
	 *     }],
	 *     "count": 6
	 * }
	 */
	public function listAction()
	{   
		$cond = [];
		if(isset($this->data['user_name']) && strlen($this->data['user_name']) > 0){
            $cond[] = ['','exp',Log::raw("locate('{$this->data['user_name']}',user_name)>0")];
        }
        if(!empty($this->data['user_type'])){
            $cond[] = ['user_type','=',$this->data['user_type']];
        }
        if(!empty($this->data['path'])){
            $cond[] = ['','exp',Log::raw("locate('{$this->data['path']}',path)>0")];
        }
        if(!empty($this->data['start'])){
            $cond[] = ['create_time','>=',strtotime($this->data['start'])];
        }
        if(!empty($this->data['end'])){
            $cond[] = ['create_time','<=',strtotime($this->data['end'])];
        }
		$list = Log::doList($cond,$this->page,$this->size);
		return parent::resSuccess($list);
	}
	
	
}
